package com.ruoyi.project.manhua.webLook.service.impl;


import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.framework.qiniu.QiNiuYunUtils;
import com.ruoyi.framework.redis.RedisKey;
import com.ruoyi.framework.redis.RedisUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.manhua.chapter.domain.MhChapter;
import com.ruoyi.project.manhua.newXml.MhChapterNewMapper;
import com.ruoyi.project.manhua.newXml.MhPageNewMapper;
import com.ruoyi.project.manhua.page.domain.MhPage;
import com.ruoyi.project.manhua.webLook.entity.MhWebChapter;
import com.ruoyi.project.manhua.webLook.service.MhpictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class MhpictureServiceImpl implements MhpictureService {


    @Autowired
    private MhChapterNewMapper mhChapterNewMapper;

    @Autowired
    private MhPageNewMapper mhPageNewMapper;

    @Autowired
    private QiNiuYunUtils qiNiuYunUtils;

    @Autowired
    private RedisUtils redisUtils;

    /*启用redis开关*/
    @Value("${spring.redis.enabled}")
    private boolean enabled;

    /**
     * 返回查看图片基本数据
     *
     * @param nameid    漫画id
     * @param nameOrder 篇章的顺醋
     * @return
     */
    @Override
    public synchronized AjaxResult pictureService(Long nameid, Integer nameOrder) {

        /*漫画全部篇章*/
        List<MhChapter> mhChapters = null;
        /*漫画 篇章页数*/
        List<String> listImg = null;

        /*漫画全部篇章*/
        if (enabled) {
            /*redis 中查询*/
            mhChapters = (List<MhChapter>) redisUtils.get(RedisKey.pictureRedis.mh_chapter_all.getKey() + nameid);
            /*没有的话 数据库查询*/
            if (CollectionUtils.isEmpty(mhChapters)) {

                mhChapters = mhChapterNewMapper.selectWebMhChapterList(new MhWebChapter(nameid, Constants.mysqlOrder.asc));
                /*添加*/
                redisUtils.set(RedisKey.pictureRedis.mh_chapter_all.getKey() + nameid, mhChapters, RedisKey.time);
            }

        } else {
            /*反之 数据库查*/
            mhChapters = mhChapterNewMapper.selectWebMhChapterList(new MhWebChapter(nameid, Constants.mysqlOrder.asc));

        }

        if (CollectionUtils.isEmpty(mhChapters)) {
            throw new BusinessException("没有此篇章^_^...");
        }

        /*首先查出所有 漫画的章节*/
        LinkedHashMap<Integer, MhChapter> collect = mhChapters
                .stream()
                .collect(Collectors.toMap(
                        (k) -> k.getNameOrder(),
                        (v) -> v,
                        (k, v) -> k,
                        LinkedHashMap::new
                ));

        /*漫画 篇章页数*/
        if (enabled) {

            listImg = (List<String>) redisUtils.get(RedisKey.pictureRedis.mh_page_img.getKey() + collect.get(nameOrder).getId());

            /*等于空*/
            if (CollectionUtils.isEmpty(listImg)) {
                listImg = mhPageNewMapper
                        .selectMhPageListOrder(nameid, collect.get(nameOrder).getId(), Constants.mysqlOrder.asc)
                        .stream().map((e) -> {
                            if (e.getSrc().indexOf("http") != -1) {
                                return Constants.getEncoder(e.getSrc());
                            } else {
                                return Constants.getEncoder(qiNiuYunUtils.getUrlImg(e.getSrc()));
                            }
                        }).collect(Collectors.toList());
                /*添加*/
                redisUtils.set(RedisKey.pictureRedis.mh_page_img.getKey() + collect.get(nameOrder).getId(), listImg, RedisKey.time);
            }
        } else {

            List<MhPage> mhPages = mhPageNewMapper.selectMhPageListOrder(nameid, collect.get(nameOrder).getId(), Constants.mysqlOrder.asc);
            if (CollectionUtils.isEmpty(mhPages)) {
                throw new BusinessException("没有此篇章^_^...");
            }

            listImg = mhPages
                    .stream().map((e) -> {
                        if (e.getSrc().indexOf("http") != -1) {
                            return Constants.getEncoder(e.getSrc());
                        } else {
                            return Constants.getEncoder(qiNiuYunUtils.getUrlImg(e.getSrc()));
                        }
                    }).collect(Collectors.toList());

        }

        AjaxResult ajaxResult = new AjaxResult();

        ajaxResult.put(AjaxResult.CODE_TAG, AjaxResult.Type.SUCCESS.value());

        ajaxResult.put("chapterCollect", collect);

        ajaxResult.put("mhPages", listImg);
        /*篇章信息*/
        ajaxResult.put("chapterName", collect.get(nameOrder).getName());

        ajaxResult.put("chapterNameName", collect.get(nameOrder).getNameName());

        ajaxResult.put("chapterImg", Constants.getDecoder(listImg.get(0)));

        return ajaxResult;
    }


    /**
     * 查看 章节 图片
     *
     * @param nameId 漫画 id
     * @param pageId 章节 id
     * @return
     */
    @Override
    public List<String> pageDataService(Long nameId, Long pageId) {

        List<String> listImg = null;
        List<MhPage> mhPages = null;

        String getRedisStr = RedisKey.pictureRedis.mh_page_img.getKey() + pageId;

        if (enabled) {

            listImg = (List<String>) redisUtils.get(getRedisStr);

            if (CollectionUtils.isEmpty(listImg)) {

                synchronized (this) {

                    listImg = (List<String>) redisUtils.get(getRedisStr);

                    if (CollectionUtils.isEmpty(listImg)) {
                        System.err.println("redis进入数据库了、、、、");
                        mhPages = mhPageNewMapper.selectMhPageListOrder(nameId, pageId, Constants.mysqlOrder.asc);

                        if (CollectionUtils.isEmpty(mhPages)) {
                            throw new BusinessException("没有此篇章^_^...");
                        }
                        /*不等于 空 拼装数据*/
                        listImg = mhPages.stream().map((e) -> {
                            if (e.getSrc().indexOf("http") != -1) {
                                return Constants.getEncoder(e.getSrc());
                            } else {
                                return Constants.getEncoder(qiNiuYunUtils.getUrlImg(e.getSrc()));
                            }
                        }).collect(Collectors.toList());
                        /*添加*/
                        redisUtils.set(RedisKey.pictureRedis.mh_page_img.getKey() + pageId, listImg, RedisKey.time);
                    }
                }

            }

        } else {

            /*查出 篇章 图片 数据*/
            mhPages = mhPageNewMapper.selectMhPageListOrder(nameId, pageId, Constants.mysqlOrder.asc);

            if (CollectionUtils.isEmpty(mhPages)) {
                throw new BusinessException("没有此篇章^_^...");
            }
            /*不等于 空 拼装数据*/
            listImg = mhPages.stream().map((e) -> {
                if (e.getSrc().indexOf("http") != -1) {
                    return Constants.getEncoder(e.getSrc());
                } else {
                    return Constants.getEncoder(qiNiuYunUtils.getUrlImg(e.getSrc()));
                }
            }).collect(Collectors.toList());
        }


        return listImg;
    }
}
